package com.giaothong.view;

import java.util.Collection;
import java.util.List;

import com.giaothong.R;
import com.giaothong.entities.Choice;
import com.giaothong.entities.Question;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * Tao ra ListAdapter co kha nang cuon tron
 * @author long
 *
 */
public class BienBaoListAdapter extends BaseAdapter {
	
	private String TAG = "BienBaoAdapter";
	private Context mContext;
	private List<Question> mListQuestion;

	public BienBaoListAdapter(Context context, List<Question> list) {
		mContext = context;
		mListQuestion = list;

	}

	@Override
	public int getCount() {
		return mListQuestion.size();
	}

	@Override
	public Object getItem(int pos) {
		return mListQuestion.get(pos);
	}

	@Override
	public long getItemId(int pos) {
		return pos;
	}

	@Override
	public View getView(int pos, View convertView, ViewGroup parent) {
		// get selected entry
		Question question = mListQuestion.get(pos);

		// inflating list view layout if null
		if(convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(mContext);
			convertView = inflater.inflate(R.layout.bienbao_islistview, null);
		}
		// set ten
		TextView bBaoTilte = (TextView)convertView.findViewById(R.id.bienbaoTitle);
		bBaoTilte.setText(question.getTitle()+"");
		Log.i(TAG,"Tile " +question.getTitle());
		
		// set bien bao
		TextView imgBienBao = (TextView)convertView.findViewById(R.id.imgBienBaogAdapter);
		int idPatchQuestion = mContext.getResources().getIdentifier(question.getImage().trim(), "drawable", mContext.getPackageName());
		if(idPatchQuestion > 0){
			Log.i(TAG,"Hinh Bien Bao" +question.getImage());
			imgBienBao.setBackgroundDrawable(mContext.getResources().getDrawable(idPatchQuestion));
		}
		// set noi dung
		TextView bBaoContent = (TextView)convertView.findViewById(R.id.bienbaoContent);
		Collection<Choice> choice = question.getChoices();
		for(Choice c : choice){
			bBaoContent.setText(c.getTitle()+"");
			Log.i(TAG,"Noi Dung"+c.getTitle());
		}
		return convertView;
	}
}